<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>AJAX </title>
</head>
<body>
    <h1>AJAX</h1>
    <div>Score :<span id="score"></span></div>
    <div id="output"></div>
    <button type="button" id="btn">Click Me for More</button>
    <script>
        var btn = document.getElementById('btn');
        btn.addEventListener('click', nextItem)
        var answers = {
            'correct': 0
            , 'wrong': 0
        }
        var output = document.getElementById('output');
        function nextItem() {
            var url = 'https://opentdb.com/api.php?amount=1&&type=boolean';
            var html = '<h2>Question</h2> '
            requestAJAX(url, function (data) {
                console.log(data.results[0]);
                var obj = data.results[0];
                html += '<div><div class="cat">' + obj.category + '</div>';
                html += '<div class="que">' + obj.question + '</div>';
                html += '</div>'
                output.innerHTML = html;
            
            })
        }
 
        function requestAJAX(url, callback) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    callback(JSON.parse(xhr.responseText))
                }
            }
            xhr.open('GET', url, true)
            xhr.send();
        }
    </script>
</body>
</html>